//
//  ZDTabBar.swift
//  1026SwiftSinaWeiBo
//
//  Created by Apple on 15/10/26.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit

class ZDTabBar: UITabBar {

    override func layoutSubviews() {
        
        //调用父类方法
        super.layoutSubviews()
        
//        print("自定义tabBar")
        
        print("\(subviews)")
        
        let width = bounds.width / 5
        let height = bounds.height
        
        var index: CGFloat = 0
        
        for view in subviews {
        
            if view is UIControl {      //UITabBarButton是苹果私有类，没有公开，这里选择其父类来做判断
            
                view.frame = CGRectMake(index * width, 0, width, height)
                
//                index++
//                
//                if index == 2 {
//                
//                    index++
//                }
                index += index == 1 ? 2 : 1
            }
        }
        
        //设置终点按钮的frame
        composeButton.frame = CGRectMake(2 * width, 0, width, height)
    }
    
    
    //MARK: - 懒加载
    private lazy var composeButton: UIButton = {
    
        let composeButton = UIButton()
        
        composeButton.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        composeButton.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        composeButton.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        composeButton.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        //在懒加载中添加  只需添加一次
        self.addSubview(composeButton)
        
        return composeButton
    }()

}
